import {
	profile
} from '../env-config.js'

import store from '@/store/store.js'
// import {
// 	api
// } from './api.js'

let dapi;
if (profile.active === "dev" || profile.active === "old") {
	dapi = {
		//----------cate--------
		/**
		 * 返回分类列表数据
		 */
		async getCateList() {
			const {
				data: res
			} = await uni.$api.getCateList()
			if (res.meta.status !== 200) return uni.$showMsg()

			return res.message
		},
		//--------subpkg/search-----
		/**
		 * 返回搜索数据
		 * @param {Object} kw 关键字
		 */
		async postSearch(kw) {
			const {
				data: res
			} = await uni.$api.postSearch(kw)
			if (res.meta.status !== 200) return uni.$showMsg()
			return res.message
		},
		//--------subpkg/goodList-----
		/**
		 * 获取商品列表
		 * @param {Object} queryObj 请求参数
		 *queryObj: {
			query: '',
			cid: '',
			pagenum: 1,
			pagesize: 10
		},
		 */
		async getGoodsList(queryObj) {
			const {
				data: res
			} = await uni.$api.getGoodsList(queryObj)
			if (res.meta.status !== 200) return uni.$showMsg()
			return res.message
		},
		//--------subpkg/goods_detail----
		//获取商品详情
		async getGoodsDetail(goodsId) {
			const {
				data: res
			} = await uni.$api.getGoodsDetail(goodsId)
			if (res.meta.status !== 200) return uni.$showMsg()
			return res.message
		},
		//---------components/my-login-------
		/**
		 * 登录
		 * @param {Object} query
		 * const query = {
			code: res.code,
			encryptedData: info.encryptedData,
			iv: info.iv,
			rawData: info.rawData,
			signature: info.signature
		}
		 */
		async postLogin(query) { //登录
			let {
				data: loginResult
			} = await uni.$api.postLogin(query)

			//*********
			// 设置虚拟结果
			loginResult = uni.$env.UserSetting.setUserLoginResult(loginResult);

			if (loginResult.meta.status !== 200) return uni.$showMsg('登录失败！')

			return loginResult.message
		},
		//---------components/my-settle---------
		// const orderInfo = {
		// 	// order_price: this.checkedGoodsAmount,
		// 	order_price: 0.01,
		// 	consignee_addr: this.addstr,
		// 	goods: this.cart.filter(x => x.goods_state).map(x => ({
		// 		goods_id: x.goods_id,
		// 		goods_number: x.goods_count,
		// 		goods_price: x.goods_price
		// 	}))
		// }
		// 1.2 发起请求创建订单
		async postCreateOrder(orderInfo) { //创建订单
			let {
				data: res
			} = await uni.$api.postCreateOrder(orderInfo)
			// console.log("订单", res);
			if (res.meta.status !== 200) return uni.$showMsg('创建订单失败！')
			return res.message
		},
		// 2. 订单预支付
		// 2.1 发起请求获取订单的支付信息
		async postReqUnifiedOrder(orderNumber) { //请求获取订单的支付信息
			// 2. 订单预支付
			// 2.1 发起请求获取订单的支付信息
			const {
				data: res2
			} = await uni.$api.postReqUnifiedOrder({
				order_number: orderNumber
			}) // 2.2 预付订单生成失败
			if (res2.meta.status !== 200) return uni.$showMsg('预付订单生成失败！')
			// 2.3 得到订单支付相关的必要参数
			return res2.message
		},
		async postCheckOrder(orderNumber) { //订单是否支付成功
			const {
				data: res3
			} = await uni.$api.postCheckOrder({
				order_number: orderNumber
			})
			// 3.4 检测到订单未支付
			if (res3.meta.status !== 200) return uni.$showMsg('订单未支付！')
			return res3.message;
		}
	}
}
//***************************
//***************************
//***************************
else if (profile.active === "prod") {
	dapi = {
		//----------cate--------
		/**
		 * 返回分类列表数据
		 */
		async getCateList() {
			const {
				data: res
			} = await uni.$api.getCateList()
			if (res.code !== 1) return uni.$showMsg(res.msg)

			return res.data
		},
		//-----------加载图片---------
		async mapImg(goods) {

			// console.log("here you are", goods);
			if (goods && goods instanceof Array && goods.length > 0) {
				// console.log("in it");
				await goods.filter(async (a) => {
					const img = await this.getImg(a.url)
					a.img = img
				})
				// console.log("here goods", goods);
				return goods
			}
			return;
		},
		//--------subpkg/search-----
		/**
		 * 返回搜索数据
		 * @param {Object} kw 关键字
		 */
		async postSearch(kw) {
			const {
				data: res
			} = await uni.$api.postSearch(kw)
			if (res.code !== 1) return uni.$showMsg(res.msg)
			// this.mapImg(res.data.records)
			return res.data
		},
		//--------subpkg/goodList-----
		/**
		 * 获取商品列表
		 * @param {Object} queryObj 请求参数
		 *queryObj: {
			query: '',
			cid: '',
			pagenum: 1,
			pagesize: 10
		},
		 */
		async getGoodsList(queryObj) {
			const {
				data: res
			} = await uni.$api.getGoodsList(queryObj)
			if (res.code !== 1) return uni.$showMsg(res.msg)
			// this.mapImg(res.data.records)
			return res.data
		},
		//--------subpkg/goods_detail----
		//获取商品详情
		async getGoodsDetail(goodsId) {
			const {
				data: res
			} = await uni.$api.getGoodsDetail(goodsId)
			if (res.code !== 1) return uni.$showMsg(res.msg)
			// if (res.data && res.data.url) {
			// 	const {
			// 		data: img
			// 	} = await uni.$api.getImg(res.data.url)
			// 	if (img) res.data.img = img
			// }


			return res.data
		},
		//---------components/my-login-------
		/**
		 * 登录
		 * @param {Object} query
		 * const query = {
			code: res.code,
			encryptedData: info.encryptedData,
			iv: info.iv,
			rawData: info.rawData,
			signature: info.signature
		}
		 */
		async postLogin(query) { //登录
			let {
				data: loginResult
			} = await uni.$api.postLogin(query)

			//*********
			// 设置虚拟结果
			// loginResult = uni.$env.UserSetting.setUserLoginResult(loginResult);

			if (loginResult.code !== 1) return uni.$showMsg('登录失败!\n' + loginResult.msg)

			return loginResult.data
		},
		// 获取用户信息
		async getUserInfo() {
			const {
				data: res
			} = await uni.$api.getUserInfo()

			if (res.code !== 1) return uni.$showMsg('获取用户信息失败!\n' + res.msg)

			return res.data
		},
		async putUserInfo(user) { //修改用户信息
			const {
				data: res
			} = await uni.$api.putUserInfo(user)

			if (res.code !== 1) return uni.$showMsg(res.msg)
			return true
		},
		// 注册
		async postReg(user) {
			let {
				data: res
			} = await uni.$api.postReg(user)
			if (res.code !== 1) return uni.$showMsg('注册失败!\n' + res.msg)
			return true

			// return res.data
		},
		async getSmsCode(phone) {
			let {
				data: res
			} = await uni.$api.getSmsCode(phone)
			if (res.code !== 1) return uni.$showMsg('注册失败!\n' + res.msg)

			return res.data
		},
		//---------components/my-settle---------
		// const orderInfo = {
		// 	// order_price: this.checkedGoodsAmount,
		// 	order_price: 0.01,
		// 	consignee_addr: this.addstr,
		// 	goods: this.cart.filter(x => x.goods_state).map(x => ({
		// 		goods_id: x.goods_id,
		// 		goods_number: x.goods_count,
		// 		goods_price: x.goods_price
		// 	}))
		// }
		// 1.2 发起请求创建订单
		async postCreateOrder(orderInfo) { //创建订单
			let {
				data: res
			} = await uni.$api.postCreateOrder(orderInfo)
			// console.log("订单", res);
			if (res.code !== 1) return uni.$showMsg('创建订单失败!\n' + res.msg)
			return res.data
		},
		// 2. 订单预支付
		// 2.1 发起请求获取订单的支付信息
		// {
		// 	order_number: orderNumber
		// }
		async postReqUnifiedOrder(orderNumber) { //请求获取订单的支付信息
			// 2. 订单预支付
			// 2.1 发起请求获取订单的支付信息
			const {
				data: res2
			} = await uni.$api.postReqUnifiedOrder({
				order_number: orderNumber
			}) // 2.2 预付订单生成失败
			if (res2.code !== 1) return uni.$showMsg('预付订单生成失败！\n' + res.msg)
			// 2.3 得到订单支付相关的必要参数
			return res2.data
		},
		async postCheckOrder(orderNumber) { //订单是否支付成功
			const {
				data: res3
			} = await uni.$api.postCheckOrder({
				order_number: orderNumber
			})
			// 3.4 检测到订单未支付
			if (res3.code !== 1) return uni.$showMsg('订单未支付！\n' + res.msg)
			return res3.data;
		},


		//-----------------------
		// 获取验证码
		async getCaptcha() {
			const {
				data: res
			} = await uni.$api.getCaptcha()
			if (res.code !== 1) return uni.$showMsg(res.msg)
			return res.data
		},
		// 获取图片
		async getImg(fileName) {
			// const response = await uni.$api.getImg(fileName)
			// const {
			// 	data: res
			// } = response;
			const res = await uni.$serialize.fetchImage("http://127.0.0.1:8080/wares/download", {
				fileName
			}, (
				config) => {
				config.header["sa-token"] = store.state.m_user.token
			})
			// const {
			// 	data: res
			// } = await uni.$api.getImg(fileName)
			if (res == undefined) {
				return;
			}
			// const url = uni.$serialize.getUrlFromResp(response)
			return res;
			// return url;
			// return uni.$serialize.binToArrayBuffer(res)
			// return uni.$serialize.blobToUrl(res)
			// return uni.$serialize.binToArrayBuffer2(res)
			// return uni.$serialize.arrayBufferToBase64(res)
		},

		// --------------order---------------
		async getAllOrders() { //全部订单
			const {
				data: res
			} = await uni.$api.getAllOrders()
			if (res.code !== 1) return uni.$showMsg(res.msg)
			return res.data
		},
		async getUnCheckOrders() { //未支付全部订单
			const {
				data: res
			} = await uni.$api.getUnCheckOrders()
			if (res.code !== 1) return uni.$showMsg(res.msg)
			return res.data
		},
		async getCheckOrders() { //已支付全部订单
			const {
				data: res
			} = await uni.$api.getCheckOrders()
			if (res.code !== 1) return uni.$showMsg(res.msg)
			return res.data
		},
		async postPay(wares) { //支付商品
			const {
				data: res
			} = await uni.$api.postPay(wares)
			if (res.code !== 1) return uni.$showMsg(res.msg)
			return res.data;
		},
		async getCheck(orderId) { //确认订单
			const {
				data: res
			} = await uni.$api.getCheck(orderId)
			if (res.code !== 1) return uni.$showMsg(res.msg)
			return res.msg
		},
		//确认过期
		async checkOrderAndClear(orderId) {
			if (orderId != "") {
				const res = await this.getCheck(orderId)
				if (res == undefined) {
					console.log("未查到订单,已清除缓存");
					uni.removeStorageSync("orderId");
					uni.removeStorageSync("orderMsg")
					return;
				}
				if (res !== "未支付") {
					console.log("支付失败,已清除缓存");
					uni.removeStorageSync("orderId");
					uni.removeStorageSync("orderMsg")
					return true;
				}
				return false;
			}
		}

	}
}


export {
	dapi
}